/*
 * @class CRefCount
 * @brief This class is a reference count class.
 * @author Woo-Hyun Kim
 * @emal woorung@nhncorp.com or woorung@gmail.com
 * @date 2008/8/6
 **/
// =============================================================
#ifndef CRefCountH
#define CRefCountH
//-----------------------------------------------------------------
template <typename T> class CSmartPtr;
//-----------------------------------------------------------------
template <typename T>
class IRefCount
{
	friend class CSmartPtr<T>;

protected:
	virtual void addRef() = 0;
	virtual void delRef() = 0;
	virtual T* getPtr() const = 0;

public:
	IRefCount() {};
	virtual ~IRefCount() {};
};
//-----------------------------------------------------------------
template <typename T>
class CRefCount : public IRefCount<T>
{
private:
	int _rc;

protected:
	virtual void addRef() {
		_rc++;
	};

	virtual void delRef() {
		assert(_rc>=0);

		_rc--;

		if(_rc<=0) destroy();
	};

	virtual T* getPtr() const {
		return ((T*)this); 
	};
	virtual void destroy() {
		if(getPtr()!=NULL) delete getPtr();
	};
	
public:
	CRefCount() {
		_rc = 0;
	};
	virtual ~CRefCount() {};
};
//-----------------------------------------------------------------
#endif
